<?php

/**
 * 默认控制器
 */
class Controller_User extends Controller_Abstract
{
	function actionRegister()
	{
		$userModel = new User();
		$registerForm = $this->_context->post('registerForm', null);
		if($registerForm){
			$userModel->attributes = $registerForm;
			if($userModel->validate()){
				if($userModel->save(false)){
					if($user = $userModel->login()){
						$albumsModel = new Album();
						$albumsModel->createDefaultAlbum($user);
						
						$this->_app->changeCurrentUser($user, 'MEMBER');
						return $this->_redirect(url('album/index'));
					}
				}
			}
		}
		
		$this->_view['userModel'] = $userModel;
		$this->_view['addressTab'] = Util::getAddrTab();
	}
	
	function actionLogin()
	{
		$userModel = new User();
		$loginForm = $this->_context->post('loginForm', null);
		if($loginForm){
			$_SESSION['loginCounts'] = (isset($_SESSION['loginCounts']))? $_SESSION['loginCounts']+1 : 1;
			$userModel->attributes = $loginForm;
			if($user = $userModel->login()){
				unset($_SESSION['showCheckCode']);
				unset($_SESSION['loginCounts']);
				$this->_app->changeCurrentUser($user, 'MEMBER');
				return $this->_redirect(url('album/index'));
			}
		}
		
		$_SESSION['showCheckCode'] = false;
		if(isset($_SESSION['loginCounts']) && $_SESSION['loginCounts'] >= 3){			//after login 3 times failed, show checkCode
			$_SESSION['showCheckCode'] = true;
		}
		$this->_view['userModel'] = $userModel;
		$this->_view['showCheckCode'] = $_SESSION['showCheckCode'];
	}
	
	function actionLogout()
	{
		if($this->_app->CurrentUser()){
			$this->_app->cleanCurrentUser();
			return $this->_redirect(url('album/index'));
		}
	}
	
	function actionAccesscode()
	{
		Util::accessCodeEntry();
	}
	
}


